﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDF.DataHelper.BussinessObject.NewsBO;
using System.Data;
using MDF.DataHelper.DataObject.NewsDO;
using MDF.Utilites.Tools;
using WebLibs;

namespace MDF.DataHelper.BussinessLogic.NewsBL
{
    public class NewsBUS : BaseBussinessLogic
    {
        #region staticMember
        /// <summary>
        /// 
        /// </summary>
        private static NewsBUS _instance;
        /// <summary>
        /// 
        /// </summary>
        public static NewsBUS Current
        {
            get { return _instance == null ? _instance = new NewsBUS() : _instance; }
        }
        #endregion
        /// <summary>
        /// Get latest news with numbers
        /// </summary>
        /// <param name="_iAmount">Amount of news</param>
        /// <param name="_iLang">Language</param>
        /// <returns>List of news</returns>
        public List<News> GetlatestNews(string _strSearch,int _iLang,int _iTypeID, int _iPage,int _iPageSize)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetAll(_strSearch, _iLang, _iTypeID, _iPage, _iPageSize);
            var lstTemp = dt.ToList<News>();
            return lstTemp;
        }
        public List<News> GetlatestNewsLimitDate(string _strSearch, int _iLang, int _iTypeID, int _iPage, int _iPageSize)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetlatestNewsLimitDate(_strSearch, _iLang, _iTypeID, _iPage, _iPageSize);
            var lstTemp = dt.ToList<News>();
            return lstTemp;
        }
        //GetlatestNewsLimitDate
        /// <summary>
        /// Get older news with numbers
        /// </summary>
        /// <param name="_iLang">Language</param>
        /// <param name="_iAmount">List of news</param>
        /// <returns></returns>
        public List<News> GetOlderNews(int _iLang, int _iNewsID, int _iAmount)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetOlderNews(_iLang, _iNewsID, _iAmount);
            var lstTemp = dt.ToList<News>();
            return lstTemp;
        }
        /// <summary>
        /// Get latest news with numbers which was pined
        /// </summary>
        /// <param name="_iAmount">Amount of news</param>
        /// <param name="_iLang">Language</param>
        /// <returns>List of news</returns>
        public List<News> GetlatestPinedNews(int _iLang, int _iAmount)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetlatestPinedNews(_iLang, _iAmount);
            var lstTemp = dt.ToList<News>();
            return lstTemp;
        }

        public News GetByID(int _iNewsID, int _iLang)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetByID(_iNewsID, _iLang);
            var lstTemp = dt.ToObject<News>();
            return lstTemp;
        }


        public bool Insert(News _nVietnam, News _nEnglish)
        {
            try
            {
                _nVietnam.Description = _nVietnam.Description.Trim().Replace("\r\n", "<br/>");
                _nEnglish.Description = _nEnglish.Description.Trim().Replace("\r\n", "<br/>");


                this.BeginTran();

                NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
                int ID = objNewsDAO.Insert(_nVietnam.ID,
                                    _nVietnam.LangID,
                                    _nVietnam.TypeID,
                                    _nVietnam.Title,
                                    _nVietnam.Link,
                                    _nVietnam.Description,
                                    _nVietnam.Content,
                                    _nVietnam.Image,
                                    _nVietnam.RefLink,
                                    _nVietnam.SEO,
                                    _nVietnam.CreatedUser,
                                    _nVietnam.IP,
                                    _nVietnam.IsPin);
                if (ID < 1)
                {
                    this.RollbackTran();
                    return false;
                }
                _nEnglish.ID = ID;
                int IDSecond = objNewsDAO.Insert(_nEnglish.ID,
                                    _nEnglish.LangID,
                                    _nEnglish.TypeID,
                                    _nEnglish.Title,
                                    _nEnglish.Link,
                                    _nEnglish.Description,
                                    _nEnglish.Content,
                                    _nEnglish.Image,
                                    _nEnglish.RefLink,
                                    _nEnglish.SEO,
                                    _nEnglish.CreatedUser,
                                    _nEnglish.IP,
                                    _nEnglish.IsPin);
                if (IDSecond == ID)
                {
                    this.CommitTran();
                }
                else
                {
                    this.RollbackTran();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }


        public bool Delete(int _iNewsID, string _strUsername)
        {
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            objNewsDAO.Delete(_iNewsID, _strUsername);
            return true;
        }

        public bool Update(News _nVietnam, News _nEnglish)
        {
            _nVietnam.Description = _nVietnam.Description.Trim().Replace("\r\n", "<br/>");
            _nEnglish.Description = _nEnglish.Description.Trim().Replace("\r\n", "<br/>");

            this.BeginTran();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);

            if (!objNewsDAO.Update(_nVietnam.ID,
                _nVietnam.LangID,
                _nVietnam.TypeID,
                _nVietnam.Title,
                _nVietnam.Link,
                _nVietnam.Description,
                _nVietnam.Content,
                _nVietnam.Image,
                _nVietnam.RefLink,
                _nVietnam.SEO,
                _nVietnam.UpdatedUser,
                _nVietnam.IP,
                _nVietnam.IsPin))
            {
                this.RollbackTran();
                return false;
            }
            if (!objNewsDAO.Update(_nEnglish.ID,
                _nEnglish.LangID,
                _nEnglish.TypeID,
                _nEnglish.Title,
                _nEnglish.Link,
                _nEnglish.Description,
                _nEnglish.Content,
                _nEnglish.Image,
                _nEnglish.RefLink,
                _nEnglish.SEO,
                _nEnglish.UpdatedUser,
                _nEnglish.IP,
                _nEnglish.IsPin))
            {
                this.RollbackTran();
                return false;
            }
            this.CommitTran();
            return true;
        }

        public List<News> GetByType(int _iTypeNews, int _iLangID, int _iPage, int _iPageSize)
        {
            DataTable dt = new DataTable();
            NewsDAO objNewsDAO = new NewsDAO(this.m_objData);
            dt = objNewsDAO.GetByType(_iTypeNews, _iLangID,_iPage,_iPageSize);
            var lstTemp = dt.ToList<News>();
            return lstTemp;
        }
       
    }
}
